#include <8052.h>
#include "uart.h"

void uart_init(void)
{
    SCON = 0b01010000;
    // PCON = 0b10000000;
    TMOD = TMOD & 0x0f | 0b00100000;
    TH1 = (char)-(int)(11030000.0 / 12 / 32 / 9600 + 0.5);
    TR1 = 1;
}

void uart_putchar(char c)
{
    SBUF = c;
    while (!TI);
    TI = 0;
}

char uart_getchar(void)
{
    char c;
    while (!RI);
    c = SBUF;
    RI = 0;
    return c;
}

void uart_writeln(const char * s)
{
    while (*s) uart_putchar(*s++);
    uart_putchar('\n');
}

void uart_readln(char * buf, int len)
{
    int i = 0;
    while (i < len - 1)
    {
        char c = uart_getchar();
        if (c == '\n') break;
        buf[i++] = c;
    }
    buf[i] = 0;
}
